package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.entity.PaymentManagement;
import com.baichang.taoshunda.service.PaymentManagementService;
import com.baichang.taoshunda.service.UserRechargeHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created by Administrator on 2018/2/1 0001.
 */
@Controller
@RequestMapping(ControllerConstants.SELECT)
public class SelectTimeWController extends WebBaseController {

    @Autowired
    private PaymentManagementService paymentManagementService;

    @Autowired
    private UserRechargeHistoryService userRechargeHistoryService;

    /**
     * @return
     */
    @RequestMapping("/findSelectTime")
    public String findSelectTime() {
        return "taoshunda/auditing/selectTime";
    }

    /**
     * 生活缴费记录
     *
     * @return
     */
    @RequestMapping("/findLifePay")
    public String findLifePay() {
        return "taoshunda/auditing/lifePay";
    }

    /**
     * 查询时间金钱
     *
     * @param request
     * @param response
     */
    @RequestMapping("/querySelectTime")
    public void queryTimeInfo(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                return paymentManagementService.queryTimeInfo(nowPage, pageSize);
            }
        });
    }

    /**
     * 添加时间金钱
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addSelectTime")
    public void addSelectTime(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer months = getInt(request, "months", 0);
                Integer monthsTwo = getInt(request, "monthsTwo", 0);
                BigDecimal money = BigDecimal.valueOf(getDouble(request, "money", 0));
                Integer type = getInt(request, "type", 0);
                String  remark = getString(request, "remark", "");
                BigDecimal oldMoney = BigDecimal.valueOf(getDouble(request, "oldMoney", 0));
                return paymentManagementService.addSelectTime(months, monthsTwo, money, type,remark,oldMoney);
            }
        });
    }

    /**
     * @param request
     * @param response
     */
    @RequestMapping("/updateSelectTime")
    public void updateSelectTime(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer months = getInt(request, "months", 0);
                BigDecimal money = BigDecimal.valueOf(getDouble(request, "money", 0));
                String  remark = getString(request, "remark", "'");
                BigDecimal oldMoney = BigDecimal.valueOf(getDouble(request, "oldMoney", 0));
                Integer type = getInt(request, "type", 0);
                return paymentManagementService.updateSelectTime(id, months, money, type,remark,oldMoney);
            }
        });
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     */
    @RequestMapping("/delete")
    public void delete(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return paymentManagementService.delete(id);
            }
        });
    }

    /**
     * 查询生活缴费记录
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryUserLife")
    public void queryUserLife(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                String telephone = getString(request, "telephone", "");
                return userRechargeHistoryService.queryUserLife(name, telephone, nowPage, pageSize);
            }
        });
    }
}
